﻿@using System
@using CodeBuilder
@using CodeBuilder.Models.TemplateModels
@inherits RazorEngine.Templating.TemplateBase<CodeBuilder.Models.TemplateModels.DtoFileModel>

@{
    var moduleNameControlName = "#" + Model.Name.FirstCharToLower() + "CreateOrEditForm";
}

<form nz-form @moduleNameControlName="ngForm" (ngSubmit)="save()" autocomplete="off">
    <div class="modal-header">
        <div class="modal-title">
            <i nz-icon type="medicine-box" class="mr-sm"></i>
            <span *ngIf="@(Model.Name.FirstCharToLower())Id">编辑@(Model.CnName)</span>
            <span *ngIf="!@(Model.Name.FirstCharToLower())Id">新增@(Model.CnName)</span>
        </div>
    </div>
    <fieldset>
        @foreach (ClassProperty classProperty in Model.ClassPropertys)
        {
            var propertyNameControlName = "#" + classProperty.Name.FirstCharToLower() + "Input";
            var required = false;
            var maxlength = 0;
            var maxlengthStr = "";

            if (classProperty.Name == "TenantId")
            {
                continue;
            }

            if (classProperty.ClassAttributes != null && classProperty.ClassAttributes.Count > 0)
            {

                foreach (var classAttribute in classProperty.ClassAttributes)
                {
                    if (classAttribute.Name == "Required")
                    {
                        required = true;
                    }
                    else if (classAttribute.Name == "MaxLength")
                    {
                        maxlength = Convert.ToInt32(classAttribute.Value.Split(',')[0]);
                    }
                }
            }

            if (maxlength > 0)
            {
                maxlengthStr = "maxlength=\""+ maxlength.ToString() +"\"";
            }

            <nz-form-item nz-row>
                <nz-form-label [nzSm]="4" nzFor="@(classProperty.Name)Input" @(required ? "nzRequired" : "")>
                    @classProperty.CnName
                </nz-form-label>
                <nz-form-control [nzSm]="20" nzHasFeedback>
                    <input nz-input @propertyNameControlName="ngModel" name="@(classProperty.Name)" [(ngModel)]="@(Model.Name.FirstCharToLower()).@classProperty.Name.FirstCharToLower()"
                           placeholder="请输入@(classProperty.CnName)" @(required ? "required" : "") @Raw(maxlengthStr)>
                    <nz-form-explain *ngIf="@(classProperty.Name.FirstCharToLower())Input.dirty && @(classProperty.Name.FirstCharToLower())Input.errors">
                        <validation-messages [formCtrl]="@(classProperty.Name.FirstCharToLower())Input"></validation-messages>
                    </nz-form-explain>
                </nz-form-control>
            </nz-form-item>
        }
    </fieldset>
    <div class="modal-footer">
        <button nz-button [nzType]="'default'" type="button" (click)="close($event)" [disabled]="saving">
            {{"Cancel" | localize}}
        </button>
        <button nz-button [nzType]="'primary'" type="submit" [disabled]="!@(Model.Name.FirstCharToLower())CreateOrEditForm.form.valid|| saving"
                [nzLoading]="saving">
            <i nz-icon type="save" *ngIf="!saving"></i> {{"Save" | localize}}
        </button>
    </div>
</form>